package com.lky.mapper;

import com.lky.domain.NewUser;
import com.lky.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    //根据id查询用户
    @Select("select * from t_user where id=#{id}")
    @Results(id = "findUserById",value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "admin",property = "admin")
    })
    User findUserById(Integer id);

    //查询全部用户
    @Select("select * from t_user")
    @Results(id = "findAllUser",value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "admin",property = "admin")
    })
    List<User> findAllUser();

    //添加用户
    @Insert("insert into t_user(username,password,phone,email,admin) " +
            "values(#{username},#{password},#{phone},#{email},#{admin})")
    int insertUser(User user);

    //添加用户-普通用户注册
    @Insert("insert into t_user(username,password,phone,email) " +
            "values(#{username},#{password},#{phone},#{email})")
    int insertUser_new(User user);

    //更新用户信息
    @Update("update t_user " +
            "set username=#{username},password=#{password},phone=#{phone},email=#{email},admin=#{admin}" +
            "where id=#{id}")
    int updateUserById(User user);

    //删除用户信息
    @Delete("delete from t_user where id=#{id}")
    int deleteUserById(Integer id);


    //根据username查询user,用户登录验证
    @Select("select * from t_user where username=#{username} and password=#{password}")
    User findUserByNameAndPassword(User user);

    //判断是否为合法注册，根据用户名查询是否存在该用户
    @Select("select * from t_user where username=#{username}")
    User findUserByName(User user);

    //模糊查询
    @Select("select * from t_user where 1=1 and username like CONCAT('%',#{searchParam},'%') " +
            "or phone like CONCAT('%',#{searchParam},'%')" +
            "or email like CONCAT('%',#{searchParam},'%')"+
            "or admin like CONCAT('%',#{searchParam},'%')")
    @Results(id = "findUserByStr",value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "admin",property = "admin")
    })
    List<User> findUserByStr(String str);


    @Insert("insert into new_t_user(t_user_id,username,password,phone,email,admin) " +
            "values(#{t_user_id},#{username},#{password},#{phone},#{email},#{admin})")
    int insertUserToNew_User(NewUser newuser);
}
